package com.superm.service.impl;

import com.superm.entity.Emp;
import com.superm.entity.Member;
import com.superm.entity.Order;
import com.superm.entity.SaleOrder;
import com.superm.mappers.MemberMapper;
import com.superm.mappers.OrderMapper;
import com.superm.mappers.SaleOrderDetailsMapper;
import com.superm.mappers.SaleOrderMapper;
import com.superm.service.OrderService;
import com.superm.service.SaleODetService;
import com.superm.service.SaleOrderService;
import com.superm.utils.EmpContent;
import com.superm.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class SaleOrderServiceImpl implements SaleOrderService {
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SaleOrderDetailsMapper saleODMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public Integer addSaleOrder(SaleOrder saleOrder) {
        return saleOrderMapper.addSaleOrder(saleOrder);
    }

    @Override
    public SaleOrder queryId(String saleOrderCode) {
        return saleOrderMapper.queryId(saleOrderCode);
    }

    @Override
    public Map<String, Object> addSaleOrder(List<Integer> ords, String paymanner, Member member) {
        Map<String,Object> map = new HashMap<>();
        map.put("success",false);
        map.put("msg","支付失败");
        DecimalFormat df = new DecimalFormat("#.00");//保留两位小数
        Emp emp = EmpContent.EmpUtils();
        List<Order> orders = new ArrayList<>();
        String goods = "";//查询多个暂存商品信息i
        String OrderCode = Tool.dateRandom("nb");//订单号
        for (Integer ord:ords) {
            goods += ord + ",";
        }
        //购买件数
        int goodsCount = 0;
        //应付金额
        double goodsYPrice=0d;
        //实付金额
        double goodsSPrice=0d;
        //优惠价格
        double salePrePrice =0f;
        goods =  goods.substring(0,goods.length() - 1);
        List<Order> orders1 = new ArrayList<>();
        orders = orderMapper.allGoodsids(goods,emp.geteId());//打印购物单要用
        for (Order o:orders) {
            goodsCount+= o.getGoodsCount();
            goodsYPrice+=o.getGoodstotlePrice();
        }
        if(member!=null){
            //折扣后的价格totalamount
            goodsSPrice = (goodsYPrice* member.getDiscount())/10;
            System.out.println(goodsSPrice);
            System.out.println("ssssssssssssssssssssssssssssss");
            System.out.println(Double.parseDouble(df.format(member.getTotalamount())));
            //优惠的价格
            salePrePrice = goodsYPrice - goodsSPrice;
            if(paymanner.equals("联合支付")){
                //减少会员全部金额
                memberMapper.upMemberMoney(member.getMemberId(),Double.parseDouble(df.format(member.getTotalamount())));
            }else{
                //减少会员指定金额
                memberMapper.upMemberMoney(member.getMemberId(),Double.parseDouble(df.format(goodsSPrice)));
            }
        }else{
            //不打折的金额
            goodsSPrice =goodsYPrice;
        }
         //获取支付方式
        //订单状态默认-1
        //下单时间  默认
        //经手人  empid
        //循环 把暂存订单的信息放到 详情订单当中
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setSaleOrderCode(OrderCode);
        saleOrder.setSaleCopePrice(goodsYPrice);
        saleOrder.setSalePaidPrice(goodsSPrice);
        saleOrder.setSaleGoodsCount(goodsCount);
        saleOrder.setSalePrePrice(Double.parseDouble(df.format(salePrePrice)));
        saleOrder.setPaymanner(paymanner);
        if(member != null){
            saleOrder.setSaleMember(member.getMemberId());
        }
        saleOrder.setEmpId(emp.geteId());
        System.out.println(saleOrder);
        //添加订单
        saleOrderMapper.addSaleOrder(saleOrder);
        //查询出来刚添加的一条订单信息
        SaleOrder saleOrder1 = saleOrderMapper.queryId(OrderCode);
        //循环插入对象信息 以实现批量添加
        for (Order o:orders) {
            o.setSaleOrderid(saleOrder1.getSaleOrderId());
            orders1.add(o);
        }
        //批量添加 订单的详情信息
        saleODMapper.addSaleDetails(orders1);
        //删除暂存订单中的信息
        if(orderMapper.delOrders(goods,emp.geteId())>0){
            map.put("success",true);
            map.put("msg","支付成功");
        }
        return map;
    }

    @Override
    public List<SaleOrder> allSaleEmpid(Integer empid) {
        return saleOrderMapper.allSaleEmpid(empid);
    }

    @Override
    public List<SaleOrder> allSaleOrder(String saleOrderCode,Integer pageNo,Integer pageSize) {
        return saleOrderMapper.allSaleOrder(saleOrderCode,(pageNo-1)*pageSize,pageSize);
    }

    @Override
    public Integer saleOrderCount(String saleOrderCode) {
        return saleOrderMapper.saleOrderCount(saleOrderCode);
    }
}
